<?php
/**
 * 搜索
 * @version 2013-11-10
 * @author ZhangHao
 */
 
class User extends MY_Controller {
	private $_data;
	
    public function __construct() {
		parent::__construct();
		$this->load->model('base_mdl', 'base');
		//$this->output->enable_profiler(TRUE);
    }

	public function reg() {

		$this->load->view(THEME.'/header', $this->_data);
		$this->load->view(THEME.'/reg', $this->_data);
		$this->load->view(THEME.'/footer');		
	}

	/**
	 * 注册
	 */
	public function do_reg() {
		$email = $this->input->post('email');
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$password2 = $this->input->post('password2');

		if(!trim($username)) output(2110, lang('account_empty'));
		if(!is_email($email)) output(1005, lang('email_error'));

		$count = $this->db->query('SELECT uid FROM mt_user WHERE email = "'.$this->db->escape_str($email).'"')->num_rows();
		if($count > 0) output(2109, lang('email_used'));
		if(strlen($password) < 6) output(2106, lang('pswd_6'));
		if($password != $password2) output(2107, lang('pswd_diff'));

		$insert_data = array(
			'username'		=> $username,
			'email'			=> $email,
			'password'		=> md5($this->config->item('userPostfix').$password),
			'status'		=> 1,
			'ctime'			=> date('Y-m-d H:i:s'),
		);
		if($this->base->insert_data('user', $insert_data)) {
			set_cookie('user', authcode(json_encode(array('uid'=>$this->db->insert_id(), 'email'=>$email, 'username'=>$username)), 'ENCODE'), 3600*24);
			output(1, '注册成功.');
		} else {
			output(100, lang('failed'));
		}
	}	
}